﻿using System.Collections.Generic;

namespace ConsoleApp.Infrastructure.Rmq
{
    public class RmqConnectPoolFactory
    {
        private static Dictionary<string, RmqConnectPool> _poolDic = new Dictionary<string, RmqConnectPool>();

        private static object lock_obj = new object();

        public static RmqConnectPool GetClientPoolString(string connect, int poolSize = 5, int checkSleepSecs = 3)
        {
            if (!_poolDic.ContainsKey(connect))
            {
                lock (lock_obj)
                {
                    if (!_poolDic.ContainsKey(connect))
                    {
                        _poolDic[connect] = new RmqConnectPool(connect, poolSize, checkSleepSecs);
                        _poolDic[connect].GetConnect();
                    }
                }
            }

            return _poolDic[connect];
        }

        public static RmqConnectPool GetClientPool(RmqOption option, int poolSize = 5, int checkSleepSecs = 3)
        {
			string connect = $"amqp://{option.UserName}:{option.Pwd}@{option.Host}:{option.Port}/{option.VirtualHost}";
            return GetClientPoolString(connect, poolSize, checkSleepSecs);
        }
    }
}
